package com.adv.ems.model.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.xssf.usermodel.XSSFColor;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

@Data
public class ReportTemplateExcelVO {
    private List<List<Info>> list;

    public ReportTemplateExcelVO() {
        this.list = new ArrayList<List<Info>>();
    }


    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Info {
        private String value;
        private Integer colspan;
        private Integer rowspan;
        private String align;
        private Color bgColor;
        private Integer rowIndex;
        private Integer colIndex;
        private Boolean isBlank;

        public Info(Integer rowIndex, Integer colIndex, Cell cell) {
            this.rowIndex = rowIndex;
            this.colIndex = colIndex;
            this.colspan = 0;
            this.rowspan = 0;
            this.align = "left";
            this.bgColor = Color.WHITE;
            this.setIsBlank(false);
            Object value = "";
            if (cell == null || cell.getCellType().toString().equals("BLANK")) {
                this.setIsBlank(true);
            }else {
                switch (cell.getCellType().toString()) {
                    case "NUMERIC":
                        value = cell.getNumericCellValue();
                        break;
                    case "STRING":
                        value = cell.getStringCellValue();
                        break;
                    default:
                        value = cell.getStringCellValue();
                        break;
                }
                CellStyle cellStyle = cell.getCellStyle();
                if (cellStyle != null) {
                    HorizontalAlignment alignment = cellStyle.getAlignment();
                    if (alignment != null && alignment.equals(HorizontalAlignment.CENTER)) {
                        this.align = "center";
                    }else if (alignment != null && alignment.equals(HorizontalAlignment.RIGHT)) {
                        this.align = "right";
                    }
                    if (cellStyle.getFillPattern() == FillPatternType.SOLID_FOREGROUND) {
                        org.apache.poi.ss.usermodel.Color fillForegroundColorColor = cellStyle.getFillForegroundColorColor();
                        if (fillForegroundColorColor != null) {
                            if (fillForegroundColorColor instanceof XSSFColor) {
                                XSSFColor c = (XSSFColor) fillForegroundColorColor;
                                byte[] rgb = c.getRGB();
                                int red = rgb[0] & 0xFF;
                                int green = rgb[1] & 0xFF;
                                int blue = rgb[2] & 0xFF;
                                this.bgColor = new Color(red, green, blue);
                            }
                        }
                    }
                }
            }
            this.value = value.toString();
        }
    }
}
